﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;
using TowerDefence_Zombie.Data;
using Sirenix.OdinInspector;
using TMPro;

namespace TowerDefence_Zombie
{

    public enum TowerLevelAnimationState
    {
        None,
        Idle,
        Attack,
        Fire,
    }

    public class EntityTowerLevel : EntityLogicEx
    {
        public Transform turret;
        public Transform[] projectilePoints;
        public Transform epicenter;
        public Launcher launcher;
        public float fireDelayTime = 0.5f;
        [Title("升级特效")]public ParticleSystem effect;

        [Title("等级")] public GameObject levelText;
        [Title("等级显示")] public TextMeshPro levelTextMesh;

        private TowerLevelAnimationState currentAnimationState = TowerLevelAnimationState.None;

        public Animator Animator;

        public AnimationClip AtkClipInfo;
        public float AtkSpeed;
        private float AtlkClipLengthTimer =0;
        protected override void OnInit(object userData)
        {
            base.OnInit(userData);
        }

        protected override void OnShow(object userData)
        {
            base.OnShow(userData);
            currentAnimationState = TowerLevelAnimationState.None;
            AtlkClipLengthTimer = 0;
            ChangeAnimationState(TowerLevelAnimationState.Idle);
        }

        public void ManualLazyShow(int level, bool isUpgrade)
        {
            levelTextMesh.text = $"{level + 1}";
            if (isUpgrade)
            {
                effect.gameObject.SetActive(false);
                effect.gameObject.SetActive(true);
            }
        }

        private void Update()
        {
            levelText.transform.LookAt(Camera.main.transform);
        }

        public void ResetTowerState(bool visible)
        {
            if (visible)
            {
                AtlkClipLengthTimer = 0;
                currentAnimationState = TowerLevelAnimationState.None;
                ChangeAnimationState(TowerLevelAnimationState.Idle);
            }
        }

        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);
            if( AtlkClipLengthTimer > 0)
                AtlkClipLengthTimer -= elapseSeconds;
        }

        protected override void OnHide(bool isShutdown, object userData)
        {
            base.OnHide(isShutdown, userData);
        }


        public void ChangeAnimationState(TowerLevelAnimationState state)
        {
             if(currentAnimationState == state || AtlkClipLengthTimer > 0) return;
            currentAnimationState = state;
            switch (state)
            {
                case TowerLevelAnimationState.Idle:
                    PlayIdelAnimation();
                    break;
                case TowerLevelAnimationState.Attack:
                    PalyAtkStateAnimation();
                    break;
                case TowerLevelAnimationState.Fire:
                    PlayFireAniamtion();
                    break;
            }
        }

        public void PlayIdelAnimation()
        {
            if (Animator != null)
            {
                Animator.CrossFade("待机",0,0,0);
            }
        }


        public void PalyAtkStateAnimation()
        {
            if (Animator != null)
            {
                Animator.CrossFade("攻击", 0, 0, 0);
            }
        }


        public void PlayFireAniamtion()
        {
            if (Animator != null)
            {
                AtlkClipLengthTimer = AtkClipInfo.length / AtkSpeed;
                Animator.CrossFade("开火", 0, 0, 0);
            }
        }
        
    }
}

